package com.zecan.blog.job;

import com.zecan.blog.dao.ArticleMapper;
import com.zecan.blog.dao.StarGoodMapper;
import com.zecan.blog.entity.GoodCount;
import com.zecan.blog.utils.BlogConstant;
import com.zecan.blog.utils.RedisUtils;
import com.zecan.blog.utils.SpringUtils;
import com.zecan.blog.utils.StringUtils;
import lombok.SneakyThrows;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 定时任务具体操作逻辑
 *
 * @ClassName AritlceJob
 * @Description
 * @date 2022/11/15 23:12
 * @Version 1.0
 */
@Component
public class UpdateArticleLikeJob extends QuartzJobBean {


    private StarGoodMapper starGoodMapper = SpringUtils.getBean(StarGoodMapper.class);


    private ArticleMapper articleMapper = SpringUtils.getBean(ArticleMapper.class);


    private RedisUtils redisUtils = SpringUtils.getBean(RedisUtils.class);



    /**
     * 每隔一小时再来处理
     * 添加点赞数量
     * corn : * * 0/1 * * ? *
     *
     * @param context
     * @throws JobExecutionException
     */
    @SneakyThrows
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        System.out.println("当前时间:" + StringUtils.dateFormat(new Date()) + "更新点赞数量定时任务开始执行");

        String[] hashAddValues = redisUtils.getAddHashValues();
        String[] hashDelValues = redisUtils.getDelHashValues();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(hashAddValues[0])) {
            //将在redis中获取所有需要添加点赞的文章 和  点赞收藏表id
            addArticleGoodCount(hashAddValues);
            // 操作结束后删除对应的redis
            redisUtils.del(BlogConstant.USER_GOOD_ARTICLE);
        } else {
            System.out.println("当前时间:" + StringUtils.dateFormat(new Date()) + "没有需要新增的文章点赞");
        }

        if (org.apache.commons.lang3.StringUtils.isNotBlank(hashDelValues[0])) {
            // 获取对应的id 并执行操作 添加操作
           delArticleGoodCount(hashDelValues);
            // 操作结束后删除对应的redis
            redisUtils.del(BlogConstant.DEL_GOOD_ARTICLE);
        } else {
            System.out.println("当前时间:" + StringUtils.dateFormat(new Date()) + "没有需要删除的文章点赞");
        }

        System.out.println("当前时间:" + StringUtils.dateFormat(new Date()) + "添加点赞定时任务执行完毕");

    }

    // 添加文章点赞逻辑
    private void addArticleGoodCount(String[] hashAddValues) {
        List<GoodCount> goodCountList = new ArrayList<>();
        for (String hashValue : hashAddValues) {
            String[] split = hashValue.split("_");
            // 截取对应的id信息
            Integer articleId = Integer.parseInt(split[0]);
            // 添加到文章的点赞数量
            goodCountList.add(GoodCount.builder().goodCount(1L).articleId(articleId).build());
        }
        //添加到数据库中
        //starGoodMapper.updateStarOrGoodStatus(goodList);
        articleMapper.updateArticleLikeCount(goodCountList);
        System.out.println("添加文章点赞数量成功");
    }

    // 减少文章点赞逻辑
    private void delArticleGoodCount(String[] hashDelValues) {
        //添加到数据库中
        List<GoodCount> goodCountList = new ArrayList<>();

        for (String hashValue : hashDelValues) {
            String[] split = hashValue.split("_");
            // 截取对应的id信息
            Integer articleId = Integer.parseInt(split[0]);
            goodCountList.add(GoodCount.builder().goodCount(-1L).articleId(articleId).build());
        }
        articleMapper.updateArticleLikeCount(goodCountList);
        System.out.println("减少文章点赞数量成功");

    }


}
